# bash /root/k8s-dev/tekton-pipeline.sh dev neolink backend neolink-cmbs-manager $GIT_URL  dev Docker-manager docker-registry.21vcloud.com/k8s-tekton/neolink-cmbs-manager $BUILD_NUMBER  neolink-manifests https://gogs.21vcloud.com/ContainerManagementPlatform/neolink-manifests.git
#ssh $remote_ip touch /tmp/taskrun_k8sdocs/k8sdocs.yaml
env=$1
echo "env: $env"
platform_name=$2
echo "platform_name: $platform_name"
project_class=$3
echo "project_class: $project_class"
project_name=$4
echo "project_name: $project_name"

git_url=$5
echo "git_url: $git_url"
# "origin/dev"
git_branch=$6
echo "git_origin_branch: $git_branch"

git_origin="origin/"
if [[ $git_branch == *$git_origin* ]]; then git_branch=${git_branch#origin/}; else echo "git_branch: $git_branch"; fi

dockerfile_name=$7
echo "dockerfile_name: $dockerfile_name"
image_url=$8
echo "image_url: $image_url"
image_tag=$9
echo "image_tag: $image_tag"

k8s_manifests_name=${10}
echo "k8s_manifests_name: $k8s_manifests_name"
k8s_manifests_url=${11}
echo "k8s_manifests_url: $k8s_manifests_url"

yaml_name="$project_name-$image_tag.yaml"
echo "yaml name: $yaml_name"

group_vars_sort_name="$env/$platform_name/$project_class/$project_name-$image_tag"

#remote_ip="10.100.2.125"
#remote_ansible_base_path="/root/my_tekton_cicd/neolink"
remote_ansible_base_path="/neolink/pipeline/neolink-dev-pipeline"
remote_group_vars_path=$remote_ansible_base_path/group_vars/$group_vars_sort_name

if [ -d  $remote_group_vars_path ]; then echo "$remote_group_vars_path exists"; else mkdir -p $remote_group_vars_path; fi



cat > "$remote_group_vars_path/$yaml_name" << EOF
# linux可执行命令路径
bin_dir: "/usr/bin"

# 流水线工程路径
#pipeline_path: "my_tekton_cicd/neolink/project/"
#pipeline_path: "/var/pipeline/"
pipeline_path: "/var/pipeline/$group_vars_sort_name/"

# 环境名 dev/uat/pre/pro
env: $env

# kubernetes info
kubernetes_api_server: https://10.150.6.24:6443
kubernetes_app_namespace: neolink-dev
kubernetes_pipeline_namespace: neolink-dev-pipeline

# argocd info
argo_server: 10.150.6.23:32732
argo_server_user: admin
argo_server_user_pass: Neolink@123@qwe
argo_app_project_name: neolink-dev

# 项目分类： front  / backend
project_class: $project_class

# 项目名称：common_support_api
project_name: $project_name

# 项目代码仓库地址：https://gogs.21vcloud.com/ContainerManagementPlatform/k8s_docs.git
git_url: $git_url

# 代码分支： master
git_branch: $git_branch

# Dockerfile name
dockerfile_name: $dockerfile_name

# 镜像地址：docker-registry.21vcloud.com/k8s-tekton/k8s-docs
image_url: $image_url

# 镜像tag ：jenkins 构建任务number
image_tag: v$image_tag

# argo cd
# 应用的manifest名称：neolink-manifests
k8s_manifests_name: $k8s_manifests_name

# 应用的manifest代码仓库地址：https://gogs.21vcloud.com/ContainerManagementPlatform/neolink-manifests.git
k8s_manifests_url: $k8s_manifests_url

EOF

#scp -pq $local_group_vars_yaml root@$remote_ip:$remote_group_vars_path

cd $remote_ansible_base_path &&  sh ./ansible-playbook-command.sh -e @group_vars/$group_vars_sort_name/$yaml_name

